package com.caoustc.okhttplib.okhttp

import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit

/**
 * 下个版本删除.
 */
@Deprecated("已废弃. 3.9.01")
object OkHttpPlatformRequest {
    fun post(
        parseString: String?,
        isCancel: Boolean,
        showOnStart: Boolean,
        showOnFailure: Boolean,
        dataClass: Class<*>?,
        tagUrl: String?,
        params: OkHttpRequestParams?,
        callback: BaseHttpRequestCallback<Any?>?
    ) {
        post(
            parseString,
            isCancel,
            showOnStart,
            showOnFailure,
            dataClass,
            tagUrl,
            params,
            OkHttpConstants.REQ_TIMEOUT.toLong(),
            callback
        )
    }

    fun post(
        parseString: String?, isCancel: Boolean, showOnStart: Boolean, showOnFailure: Boolean,
        dataClass: Class<*>?, tagUrl: String?, params: OkHttpRequestParams?, timeout: Long,
        callback: BaseHttpRequestCallback<Any?>?
    ) {
        val builder: OkHttpClient.Builder? =  OkHttpManager.instance?.okHttpClientBuilder
        builder?.readTimeout(timeout, TimeUnit.MILLISECONDS)
        builder?.connectTimeout(timeout, TimeUnit.MILLISECONDS)
        builder?.writeTimeout(timeout, TimeUnit.MILLISECONDS)
        executeRequest(
            parseString, isCancel, showOnStart, showOnFailure, dataClass, OkHttpMethod.POST,
            tagUrl, params, builder!!, callback
        )
    }

    fun execute(
        method: OkHttpMethod, parseString: String?, isCancel: Boolean, showOnStart: Boolean,
        showOnFailure: Boolean, dataClass: Class<*>?, tagUrl: String?, params: OkHttpRequestParams?,
        timeout: Long, callback: BaseHttpRequestCallback<Any?>?
    ) {
        val builder: OkHttpClient.Builder? =
            OkHttpManager.instance?.okHttpClientBuilder
        builder?.readTimeout(timeout, TimeUnit.MILLISECONDS)
        builder?.connectTimeout(timeout, TimeUnit.MILLISECONDS)
        builder?.writeTimeout(timeout, TimeUnit.MILLISECONDS)
        executeRequest(
            parseString, isCancel, showOnStart, showOnFailure, dataClass, method,
            tagUrl, params, builder!!, callback
        )
    }

    private fun executeRequest(
        parseString: String?, isCancel: Boolean, showOnStart: Boolean,
        showOnFailure: Boolean, dataClass: Class<*>?, method: OkHttpMethod,
        tagUrl: String?, params: OkHttpRequestParams?,
        builder: OkHttpClient.Builder, callback: BaseHttpRequestCallback<Any?>?
    ) {
        var builder: OkHttpClient.Builder? = builder
        val platformUrl: String? = OkHttpManager.instance?.platformUrl
        if (platformUrl == null) {
            callback?.onFailure(Constants.ERROR_RESPONSE_NULL, "url is empty !")
            return
        }
        if (builder == null) {
            builder = OkHttpManager.instance?.okHttpClientBuilder
        }
        val task = OkHttpTask(isCancel, method, platformUrl, params, builder, callback)
        task.setParseKey(parseString)
        task.setDataClass(dataClass, showOnFailure)
        task.setCurrentTag(System.nanoTime().toInt())
        task.execute(showOnStart, tagUrl)
    }
}